<!DOCTYPE html>
<html lang="en">
<head xmlns:th="http://www.thymeleaf.org">
    <meta charset="UTF-8">
    <title>课程信息管理页面</title>
    <link rel="stylesheet" href="/css/bootstrap.min.css">

    <script src="/js/jquery-3.6.0.min.js"></script>
    <link rel="stylesheet" href="/plugins/bootstraptable/bootstrap-table.min.css">


</head>
<body>
<div class="container-fluid">


    <!-- Button trigger modal -->
    <button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal" onclick="preAdd();">
        新增课程
    </button>


    <br>这是成绩信息表单：</br>

    <form class="form-inline">
        <div class="form-group">
            <label for="condition">请输入课程名</label>
            <input type="text" class="form-control" id="condition" name="condition" placeholder="请输入课程名">
        </div>
        <button type="button" class="btn btn-default btn-primary" onclick="search();">查询</button>
    </form>

    <table class="table table-striped" id="couTable">

    </table>
</div>

<div class="modal fade" tabindex="-1" role="dialog" id="myModal">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title">课程信息</h4>
            </div>
            <div class="modal-body">
                <form id="formCourse">


                    <input hidden="hidden" id="id" name="id"/>  <!--隐藏读取id-->
                    <div class="form-group">
                        <label for="courseId">课程号</label>
                        <input type="text" class="form-control" id="courseId" name="courseId"  placeholder="课程号">
                    </div>
                    <div class="form-group">
                        <label for="courseName">课程名</label>
                        <input type="text" class="form-control" id="courseName" name="courseName" placeholder="课程名">
                    </div>
                    <div class="form-group">
                        <label for="chour">学时</label>
                        <input type="text" class="form-control" id="chour" name="chour"  placeholder="学时">
                    </div>
                    <div class="form-group">
                        <label for="credit">学分</label>
                        <input type="text" class="form-control" id="credit" name="credit"  placeholder="学分">
                    </div>
                </form>
                <!--表单结束-->

            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-primary" onclick="saveScore();">保存</button>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->


</div>


<script src="/js/bootstrap.min.js"></script>
<script src="/plugins/bootstraptable/bootstrap-table.min.js"></script>
<script src="/plugins/bootstraptable/locale/bootstrap-table-zh-CN.min.js"></script>


<script type="text/javascript">


    //查找
    function search() {

        $('#couTable').bootstrapTable("destroy");   //删除原来新建的表格,then重建表格
        loadTable();
    }

    function preAdd(){
        $("#id").val(0);//提示新增
    }


    //新增或保存
    function saveScore(){
        $('#couTable').bootstrapTable("destroy");

        var data=$("#formCourse").serialize();//提交的新学生信息

        var id=$("#id").val();
        if(id<1){
            //新增id
            $.ajax({
                url:"/api/course/insert",
                method:"post",
                data:data
            }).done(function (){
                loadTable();
                $('#myModal').modal('hide')
            })
        }else {
            //更新id
            $.ajax({
                url: "/api/course/update",
                method: "put",
                data: data
            }).done(function () {
                loadTable();
                $('#myModal').modal('hide')
            })
        }
    }


    //编辑
    function editScore(id){
        //$('#stuTable').bootstrapTable("destroy");

        $('#myModal').modal('show');
        $.ajax({
            url:'/api/course/get/'+id
        }).done(function (rs){

            $("#id").val(rs.id);
            $("#courseId").val(rs.courseId);
            $("#courseName").val(rs.courseName);
            $("#chour").val(rs.chour);
            $("#credit").val(rs.credit);


        })

    }

    //删除
    function  deleteScore(id){
        $('#stuTable').bootstrapTable("destroy");

        if(confirm("是否删除")){
            $.ajax({
                url:'/api/course/delete/'+id,
                method:"delete"

            }).done(function (){
                loadTable();

            });
        }

    }



    function loadTable(){
        $('#couTable').bootstrapTable({
            url: '/api/course/getbypageCourse',
            striped: true, //隔行变色
            pagination: true, //是否分页
            singleSelect: true, //禁用多选
            pageSize: 3,    //每页数据条数
            pageNumber: 1,  //首页页码
            sidePagination: "server",   //服务器端分页
            queryParams:function (params){

                var paraObj = {
                    size:params.limit,
                    page:params.offset/params.limit,
                    sort:"id",
                    direct:"desc",
                    condition: $("#condition").val()    //取到searchName框中的值
                };

                return paraObj;
            },
            columns: [{
                field: 'id',
                title: '序号'
            },{
                field: 'courseId',
                title: '课程号'
            }, {
                field: 'courseName',
                title: '课程名'
            },{
                field: 'chour',
                title: '学时'
            }, {
                field: 'credit',
                title: '学分'
            }, {
                filed:'operate',
                title: '删除列',
                formatter:function (valu,row,index){
                    var r='<a class="romove" href="#" title="Remove" onclick="deleteScore(\''+row.id+'\')">删除</a>';
                    return r;
                }
            },{
                filed:'operate',
                title: '编辑列',
                formatter:function (valu,row,index){
                    var r='<a class="Edit" href="#" title="Edit" onclick="editScore(\''+row.id+'\')">编辑</a>';
                    return r;
                }
            }]
        });


    }//end of loadTable()


    $(function (){
        loadTable();
    })

</script>
</body>
</html>